﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Baosight.Platform.Core.ServiceModel;
using Microsoft.Practices.Unity;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCP.UI.PopWindows;

namespace Baosight.ZNH.ZMCP.UI
{
    /// <summary>
    /// 作业人员管理
    /// </summary>
    /// Title:ZMCP61
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2011-07-05  高强   [创建]
    ///  
    public partial class ZMCP61 : EFViewCmes
    {
        string departmentId;
        string groupId;
        string groupLeader;
        public ZMCP61()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.MemberGrid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Platform.Core.EIInfo initData)
        {
            base.Initialize(initData);
            departmentId = initData.Get("fdDepartmentId").ToString();
            groupId = initData.Get("fdGroupId").ToString();
            groupLeader = initData.Get("fdGroupLeaderId").ToString();            
        }

        /// <summary>
        /// 新增按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "班组成员-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            AddWorkNum addWorkNum = new AddWorkNum(_dialogType);//构造一个新的修改页面
            addWorkNum.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cp.department"));
            addWorkNum.EIDataContext.Update(_eIInfo);
            addWorkNum.fdMemberOrg.Value = departmentId;
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = addWorkNum;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 删除前的判断：不能删除班组长
        /// </summary>
        /// <param name="_eIRows"></param>
        /// <returns></returns>
        public override bool BeforeMethod(List<EIRow> _eIRows)
        {
            foreach (EIRow row in _eIRows)
            {
                if (row["fdName"].Equals(groupLeader))
                {
                    BaseTools.OpenEFWindowAlert("班组长不能删除，请重新选择!", null);
                    this.TempButtonOpen(true);
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 弹出窗口关闭后执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //EIInfo eiInfo = new EIInfo();
                //eiInfo.Set("fdKeyId", groupId);
                this.UpdateHomePageData(new EIInfo());//发布数据
                this.MemberGrid.Refresh(); //刷新Grid区域
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 删除事件的回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DoDeleteOperationCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                string _msg;
                if (inInfo.GetMsg() == null || inInfo.GetMsg() == "")
                {
                    _msg = "操作失败！";
                }
                else
                {
                    _msg = inInfo.GetMsg();
                }

                this.OpenEFWindowAlert(_msg, true);//列表中删除，操作失败后按钮生效
            }
            else
            {
                base.UpdateData(e.EIInfo);
                this.TempButtonOpen(true);//列表中删除，操作成功后按钮生效
                //EIInfo eiInfo = new EIInfo();
                //eiInfo.Set("fdKeyId", groupId);
                this.UpdateHomePageData(new EIInfo());//发布数据
                this.AfterMethod(e.EIInfo);
            }

        }

        /// <summary>
        /// 重写基类方法：根据数据的改变更新主页面的数据
        /// </summary>
        /// <param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }
    }
}
